import { Loading, Message, MessageBox } from "element-ui";
import { isFunction, isArray, isString, isNumber, array, string, int } from "../util";

export default {
    name: "plugin-modal",
    async loading(loading) {
        if (loading) Loading.service();
        else Loading.service().close();
    },
    async toast(content, icon, duration) {
        if (!isString(content)) content = string(content);
        if (!isString(icon)) icon = string(icon);
        if (!isNumber(duration)) duration = int(duration) || 2500;
        if (content) return new Promise((resolve) => Message.closeAll() + Message({
            message: content,
            type: icon || "info",
            dangerouslyUseHTMLString: true,
            duration: Math.max(duration, 500),
            showClose: true,
            onClose: () => resolve(),
        }));
    },
    async alert(title, content, icon, buttons) {
        if (!isString(title)) title = string(title);
        if (!isString(content)) content = string(content);
        if (!isString(icon)) icon = string(icon);
        if (!isArray(buttons)) buttons = array(buttons);
        return new Promise((resolve) => MessageBox.alert(content, title, {
            dangerouslyUseHTMLString: true,
            type: icon || "info",
            showClose: !["warning", "error"].includes(icon),
            distinguishCancelAndClose: true,
            showConfirmButton: true,
            confirmButtonText: buttons[0] || "确定",
            showCancelButton: buttons.length > 1,
            cancelButtonText: buttons[1] || "取消",
            closeOnClickModal: false,
            closeOnPressEscape: true,
            closeOnHashChange: true,
            callback: (action) => setTimeout(() => resolve(action === "confirm"), 200),
        }));
    },
    async prompt(title, content, placeholder, value, buttons, validator) {
        if (!isString(title)) title = string(title);
        if (!isString(content)) content = string(content);
        if (!isString(placeholder)) placeholder = string(placeholder);
        if (!isString(value)) value = string(value);
        if (!isArray(buttons)) buttons = array(buttons);
        if (!isFunction(validator)) validator = (value) => value.length >= 0;
        return new Promise((resolve) => MessageBox.prompt(string(content), string(title), {
            dangerouslyUseHTMLString: true,
            showClose: true,
            distinguishCancelAndClose: true,
            showConfirmButton: true,
            confirmButtonText: buttons[0] || "确定",
            showCancelButton: true,
            cancelButtonText: buttons[1] || "取消",
            closeOnClickModal: false,
            closeOnPressEscape: true,
            closeOnHashChange: true,
            inputPlaceholder: placeholder || "请在此输入",
            inputType: "text",
            inputValue: value,
            inputValidator: validator,
            callback: (action, { inputValue: value }) => setTimeout(() => action === "confirm" ? resolve(value) : resolve(), 200),
        }));
    },
};